#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import datetime

from flask import current_app, request

from apps.common import BaseMethodView
from apps.news.control import NewsServices
from apps.news.urls import news_bp
from init_extends.init_extends import scheduler
from init_extends.schedule_tasks.tasks import async_create_news_info
from utils.model_to_dict import model_to_dict


class NewsManageView(BaseMethodView):

    @news_bp.doc(description='获取当日前 10 热门新闻')
    def get(self, **kwargs):
        count = int(request.args.get("count", "10"))
        date = datetime.datetime.now()
        data_list = NewsServices().get_news_by_hot_score(date, count)
        # NewsServices().get_news_by_enduring_hot_score()
        table_head = ["ID", "标题", "浏览量", "热搜值", "排名变化", "文章类型", "文章来源"]
        return {"head": table_head, "table": data_list}

    @news_bp.doc(description='手动执行同步新闻')
    def post(self, **kwargs):
        cur_app = current_app._get_current_object()
        scheduler.add_job(id="async_create_news_info", func=async_create_news_info, args=(cur_app,))
        return 'ok'


class StatisticsNewsView(BaseMethodView):

    @news_bp.doc(description='查询统计新闻的数据')
    def get(self, **kwargs):
        return 'ok'
